<template>
  <div class="user-container">
    <vab-query-form>
      <vab-query-form-top-panel>
        <el-form
          ref="form"
          :inline="true"
          label-width="49px"
          :model="queryForm"
          @submit.native.prevent
        >
          <el-form-item>
            <el-button icon="el-icon-plus" type="primary" @click="handleAdd">
              添加
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
    </vab-query-form>

    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column
        align="center"
        label="ID"
        prop="id"
        show-overflow-tooltip
        width="95"
      />

      <el-table-column
        align="center"
        label="员工昵称"
        prop="nickname"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        label="员工姓名"
        prop="realname"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        label="联系方式"
        prop="phone"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        label="角色名称"
        prop="role_name"
        show-overflow-tooltip
      />

      <el-table-column align="center" label="账号状态" show-overflow-tooltip>
        <template #default="{ row }">
          <el-tag :type="row.status | statusFilter">
            {{ row.status_name }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="创建时间"
        prop="created_at"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="85"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <user-edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList, doDelete } from '@/api/user'
  import UserEdit from './components/UserEdit'
  export default {
    name: 'User',
    components: {
      UserEdit,
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          Y: 'success',
          N: 'danger',
        }
        return statusMap[status]
      },
      isSuperFilter(is_super) {
        const isSuperMap = {
          Y: 'success',
          N: 'danger',
        }
        return isSuperMap[is_super]
      },
    },
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          page: 1,
          pageSize: 10,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleAdd() {
        this.$refs['edit'].showEdit()
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      handleDelete(row) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          const { msg } = await doDelete({ ids: row.id })
          this.$baseMessage(msg, 'success', 'vab-hey-message-success')
          await this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.page = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const {
          data: { list, total },
        } = await getList(this.queryForm)
        this.list = list
        this.total = total
        this.listLoading = false
      },
    },
  }
</script>

<style lang="scss" scoped></style>
